package com.hzw.saas.web.app.old.storage.controller;


import com.hzw.saas.api.group.IGroupMemberService;
import com.hzw.saas.api.group.IGroupService;
import com.hzw.saas.api.group.bo.GroupBo;
import com.hzw.saas.api.group.bo.GroupSummaryBo;
import com.hzw.saas.api.old.storage.IGroupResourceService;
import com.hzw.saas.common.config.annotation.SysLog;
import com.hzw.saas.common.security.pojo.dto.SaasUser;
import com.hzw.saas.common.security.utils.SecurityUtils;
import com.hzw.saas.service.old.storage.annotation.GroupAuthBefore;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;

/**
 * @author zzl
 * @since 01/10/2021
 */
// @RestController
@AllArgsConstructor
@Slf4j
// @Api(tags = "群组管理/群组信息")
public class GroupInfoController {

    private final MapperFacade mapperFacade;
    private final IGroupService groupService;
    private final IGroupMemberService groupMemberService;
    private final IGroupResourceService groupResourceService;

    @GetMapping("/group/summary/{groupId}")
    @SysLog
    @GroupAuthBefore(groupId = "#groupId")
    @ApiOperation(value = "获取群组概览", notes = "获取群组概览信息：群组名称、群组介绍、共享资源数量、本人发布/共享的资源数量、群成员数量")
    @ApiImplicitParam(name = "groupId", value = "群组id", required = true, paramType = "path")
    public ResponseEntity<GroupSummaryBo> getGroupSummary(@PathVariable String groupId) {
        SaasUser saasUser = SecurityUtils.getUser();
        GroupBo groupDto = groupService.getGroupById(groupId);
        GroupSummaryBo groupSummaryDto = mapperFacade.map(groupDto, GroupSummaryBo.class);
        // 获取群组成员数量
        int memberCount = groupMemberService.getMemberCountByGroupId(groupId);
        groupSummaryDto.setGroupMemberCount(memberCount);
        // 获取群组共享资源数量
        int totalSharedCount = groupResourceService.countGroupFile(groupId, null);
        groupSummaryDto.setTotalSharedCount(totalSharedCount);
        // 获取用户在群组中共享的资源数量
        int userSharedCount = groupResourceService.countGroupFile(groupId, saasUser.getUserId());
        groupSummaryDto.setUserSharedCount(userSharedCount);
        return ResponseEntity.ok(groupSummaryDto);
    }

}
